package org.admanager.soapattern.plugin.diagram.navigator;

import org.admanager.soadmanagement.plugin.soadmanagement.PComponentReference;
import org.admanager.soadmanagement.plugin.soadmanagement.PComponentService;
import org.admanager.soadmanagement.plugin.soadmanagement.PMultiplicity;
import org.admanager.soadmanagement.plugin.soadmanagement.PReference;
import org.admanager.soadmanagement.plugin.soadmanagement.PService;
import org.admanager.soadmanagement.plugin.soadmanagement.Pattern;
import org.admanager.soapattern.plugin.diagram.edit.parts.PComponent2EditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PComponentEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PComponentName2EditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PComponentNameEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PComponentReference2EditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PComponentReferenceEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PComponentReferenceTargetEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PComponentService2EditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PComponentServiceEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PCompositeEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PCompositeNameEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PConstraintEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PConstraintNameEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PElementMultiplicityEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PElementRolesEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PMultiplicityEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PReferenceEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PReferencePromoteEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PRoleEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PRoleNameEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PServiceEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PServicePromoteEditPart;
import org.admanager.soapattern.plugin.diagram.edit.parts.PatternEditPart;
import org.admanager.soapattern.plugin.diagram.part.SOADManagementMetamodelDiagramEditorPlugin;
import org.admanager.soapattern.plugin.diagram.part.SOADManagementMetamodelVisualIDRegistry;
import org.admanager.soapattern.plugin.diagram.providers.SOADManagementMetamodelElementTypes;
import org.admanager.soapattern.plugin.diagram.providers.SOADManagementMetamodelParserProvider;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

/**
 * @generated
 */
public class SOADManagementMetamodelNavigatorLabelProvider extends
		LabelProvider implements ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		SOADManagementMetamodelDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		SOADManagementMetamodelDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof SOADManagementMetamodelNavigatorItem
				&& !isOwnView(((SOADManagementMetamodelNavigatorItem) element)
						.getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof SOADManagementMetamodelNavigatorGroup) {
			SOADManagementMetamodelNavigatorGroup group = (SOADManagementMetamodelNavigatorGroup) element;
			return SOADManagementMetamodelDiagramEditorPlugin.getInstance()
					.getBundledImage(group.getIcon());
		}

		if (element instanceof SOADManagementMetamodelNavigatorItem) {
			SOADManagementMetamodelNavigatorItem navigatorItem = (SOADManagementMetamodelNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (SOADManagementMetamodelVisualIDRegistry.getVisualID(view)) {
		case PElementRolesEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://soageneralpattern/1.0?PElement?roles", SOADManagementMetamodelElementTypes.PElementRoles_4006); //$NON-NLS-1$
		case PComponent2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://soageneralpattern/1.0?PComponent", SOADManagementMetamodelElementTypes.PComponent_3013); //$NON-NLS-1$
		case PRoleEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://soageneralpattern/1.0?PRole", SOADManagementMetamodelElementTypes.PRole_2008); //$NON-NLS-1$
		case PMultiplicityEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://soageneralpattern/1.0?PMultiplicity", SOADManagementMetamodelElementTypes.PMultiplicity_2007); //$NON-NLS-1$
		case PComponentEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://soageneralpattern/1.0?PComponent", SOADManagementMetamodelElementTypes.PComponent_2005); //$NON-NLS-1$
		case PConstraintEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://soageneralpattern/1.0?PConstraint", SOADManagementMetamodelElementTypes.PConstraint_3016); //$NON-NLS-1$
		case PCompositeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://soageneralpattern/1.0?PComposite", SOADManagementMetamodelElementTypes.PComposite_2006); //$NON-NLS-1$
		case PServicePromoteEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://soageneralpattern/1.0?PService?promote", SOADManagementMetamodelElementTypes.PServicePromote_4009); //$NON-NLS-1$
		case PComponentReferenceEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://soageneralpattern/1.0?PComponentReference", SOADManagementMetamodelElementTypes.PComponentReference_3010); //$NON-NLS-1$
		case PatternEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?http://soageneralpattern/1.0?Pattern", SOADManagementMetamodelElementTypes.Pattern_1000); //$NON-NLS-1$
		case PServiceEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://soageneralpattern/1.0?PService", SOADManagementMetamodelElementTypes.PService_3012); //$NON-NLS-1$
		case PComponentReferenceTargetEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://soageneralpattern/1.0?PComponentReference?target", SOADManagementMetamodelElementTypes.PComponentReferenceTarget_4010); //$NON-NLS-1$
		case PElementMultiplicityEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://soageneralpattern/1.0?PElement?multiplicity", SOADManagementMetamodelElementTypes.PElementMultiplicity_4007); //$NON-NLS-1$
		case PComponentReference2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://soageneralpattern/1.0?PComponentReference", SOADManagementMetamodelElementTypes.PComponentReference_3015); //$NON-NLS-1$
		case PComponentServiceEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://soageneralpattern/1.0?PComponentService", SOADManagementMetamodelElementTypes.PComponentService_3009); //$NON-NLS-1$
		case PComponentService2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://soageneralpattern/1.0?PComponentService", SOADManagementMetamodelElementTypes.PComponentService_3014); //$NON-NLS-1$
		case PReferenceEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://soageneralpattern/1.0?PReference", SOADManagementMetamodelElementTypes.PReference_3011); //$NON-NLS-1$
		case PReferencePromoteEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://soageneralpattern/1.0?PReference?promote", SOADManagementMetamodelElementTypes.PReferencePromote_4008); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = SOADManagementMetamodelDiagramEditorPlugin
				.getInstance().getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null
				&& elementType != null
				&& SOADManagementMetamodelElementTypes
						.isKnownElementType(elementType)) {
			image = SOADManagementMetamodelElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof SOADManagementMetamodelNavigatorGroup) {
			SOADManagementMetamodelNavigatorGroup group = (SOADManagementMetamodelNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof SOADManagementMetamodelNavigatorItem) {
			SOADManagementMetamodelNavigatorItem navigatorItem = (SOADManagementMetamodelNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (SOADManagementMetamodelVisualIDRegistry.getVisualID(view)) {
		case PElementRolesEditPart.VISUAL_ID:
			return getPElementRoles_4006Text(view);
		case PComponent2EditPart.VISUAL_ID:
			return getPComponent_3013Text(view);
		case PRoleEditPart.VISUAL_ID:
			return getPRole_2008Text(view);
		case PMultiplicityEditPart.VISUAL_ID:
			return getPMultiplicity_2007Text(view);
		case PComponentEditPart.VISUAL_ID:
			return getPComponent_2005Text(view);
		case PConstraintEditPart.VISUAL_ID:
			return getPConstraint_3016Text(view);
		case PCompositeEditPart.VISUAL_ID:
			return getPComposite_2006Text(view);
		case PServicePromoteEditPart.VISUAL_ID:
			return getPServicePromote_4009Text(view);
		case PComponentReferenceEditPart.VISUAL_ID:
			return getPComponentReference_3010Text(view);
		case PatternEditPart.VISUAL_ID:
			return getPattern_1000Text(view);
		case PServiceEditPart.VISUAL_ID:
			return getPService_3012Text(view);
		case PComponentReferenceTargetEditPart.VISUAL_ID:
			return getPComponentReferenceTarget_4010Text(view);
		case PElementMultiplicityEditPart.VISUAL_ID:
			return getPElementMultiplicity_4007Text(view);
		case PComponentReference2EditPart.VISUAL_ID:
			return getPComponentReference_3015Text(view);
		case PComponentServiceEditPart.VISUAL_ID:
			return getPComponentService_3009Text(view);
		case PComponentService2EditPart.VISUAL_ID:
			return getPComponentService_3014Text(view);
		case PReferenceEditPart.VISUAL_ID:
			return getPReference_3011Text(view);
		case PReferencePromoteEditPart.VISUAL_ID:
			return getPReferencePromote_4008Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getPElementRoles_4006Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getPComponent_3013Text(View view) {
		IParser parser = SOADManagementMetamodelParserProvider.getParser(
				SOADManagementMetamodelElementTypes.PComponent_3013, view
						.getElement() != null ? view.getElement() : view,
				SOADManagementMetamodelVisualIDRegistry
						.getType(PComponentName2EditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SOADManagementMetamodelDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5011); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPRole_2008Text(View view) {
		IParser parser = SOADManagementMetamodelParserProvider.getParser(
				SOADManagementMetamodelElementTypes.PRole_2008, view
						.getElement() != null ? view.getElement() : view,
				SOADManagementMetamodelVisualIDRegistry
						.getType(PRoleNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SOADManagementMetamodelDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5014); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPMultiplicity_2007Text(View view) {
		PMultiplicity domainModelElement = (PMultiplicity) view.getElement();
		if (domainModelElement != null) {
			return String.valueOf(domainModelElement.getLowerBound());
		} else {
			SOADManagementMetamodelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 2007); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPComponent_2005Text(View view) {
		IParser parser = SOADManagementMetamodelParserProvider.getParser(
				SOADManagementMetamodelElementTypes.PComponent_2005, view
						.getElement() != null ? view.getElement() : view,
				SOADManagementMetamodelVisualIDRegistry
						.getType(PComponentNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SOADManagementMetamodelDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5010); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPConstraint_3016Text(View view) {
		IParser parser = SOADManagementMetamodelParserProvider.getParser(
				SOADManagementMetamodelElementTypes.PConstraint_3016, view
						.getElement() != null ? view.getElement() : view,
				SOADManagementMetamodelVisualIDRegistry
						.getType(PConstraintNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SOADManagementMetamodelDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5013); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPComposite_2006Text(View view) {
		IParser parser = SOADManagementMetamodelParserProvider.getParser(
				SOADManagementMetamodelElementTypes.PComposite_2006, view
						.getElement() != null ? view.getElement() : view,
				SOADManagementMetamodelVisualIDRegistry
						.getType(PCompositeNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SOADManagementMetamodelDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5012); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPServicePromote_4009Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getPComponentReference_3010Text(View view) {
		PComponentReference domainModelElement = (PComponentReference) view
				.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			SOADManagementMetamodelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 3010); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPattern_1000Text(View view) {
		Pattern domainModelElement = (Pattern) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			SOADManagementMetamodelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 1000); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPService_3012Text(View view) {
		PService domainModelElement = (PService) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			SOADManagementMetamodelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 3012); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPComponentReferenceTarget_4010Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getPElementMultiplicity_4007Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getPComponentReference_3015Text(View view) {
		PComponentReference domainModelElement = (PComponentReference) view
				.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			SOADManagementMetamodelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 3015); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPComponentService_3009Text(View view) {
		PComponentService domainModelElement = (PComponentService) view
				.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			SOADManagementMetamodelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 3009); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPComponentService_3014Text(View view) {
		PComponentService domainModelElement = (PComponentService) view
				.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			SOADManagementMetamodelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 3014); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPReference_3011Text(View view) {
		PReference domainModelElement = (PReference) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			SOADManagementMetamodelDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 3011); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPReferencePromote_4008Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return PatternEditPart.MODEL_ID
				.equals(SOADManagementMetamodelVisualIDRegistry
						.getModelID(view));
	}

}
